IF...THEN...ELSE Statement ---------------------------------------------------------------------------- Action Allows conditional execution, based on the evaluation of a Boolean expression. Syntax 1 IF condition THEN thenpart - ELSE elsepart- Syntax 2 IF condition1 THEN - statementblock-1- - ELSEIF condition2 THEN - statementblock-2-- - ELSE - statementblock-n-- END IF Remarks Single-Line IF...THEN...ELSE The single-line form of the statement (Syntax 1) is best used for short, straightforward tests where only one action is taken. The single-line form is never required. Any program using single-line IF... THEN... ELSE statements can be written using block form. The following list describes the parts of the single-line form. ----------------------------------------------------------------------------- Part Description ---------------------------------------------------------------------------- condition Any expression that BASIC evaluates as true (nonzero) or false (zero). thenpart, elsepart The statements or branches performed when condition is true ( thenpart) or false ( elsepart). Part Description ---------------------------------------------------------------------------- ( thenpart) or false ( elsepart). Both parts have the same syntax, which is described below. The thenpart and the elsepart fields both have the following syntax. { statements | - GOTO- linenumber | GOTO linelabel } The following list describes the parts of the thenpart and elsepart syntax. ----------------------------------------------------------------------------- Part Description ---------------------------------------------------------------------------- statements One or more BASIC statements, separated by colons. linenumber A valid BASIC program line number. linelabel A valid BASIC program line label. Note that GOTO is optional with a line number, but is required with a line label. The thenpart is executed if condition is true; if condition is false, elsepart is executed. If the ELSE clause is not present, control passes to the next statement in the program. You can have multiple statements with a condition, but they must be on the same line and separated by colons, as in the following statement. IF A > 10 THEN A=A+1.B=B+A.LOCATE 10,22.PRINT B,A Block IF...THEN...ELSE The block form (Syntax 2) provides several advantages. - The block form provides more structure and flexibility than the single-line form by allowing conditional branches across several lines. - With the block form, more complex conditions can be tested. - The block form lets you use longer statements and structures within the THEN... ELSE portion of the statement. - The block form allows your program's structure to be guided by logic rather than by how many statements fit on a line. Programs that use block IF... THEN... ELSE are usually easier to read, maintain, and debug. The following list describes the parts of the block IF... THEN... ELSE. ----------------------------------------------------------------------------- ---------------------------------------------------------------------------- condition1, condition2 Any expression that BASIC evaluates as true (nonzero) or false (zero). statementblock-1, statementblock-2 One or more BASIC statements on one , statementblock-n or more lines. In executing a block IF, BASIC tests condition1, the first Boolean expression. If the Boolean expression is true (nonzero), the statements following THEN are executed. If the first Boolean expression is false (zero), BASIC begins evaluating each ELSEIF condition in turn. When BASIC finds a true condition, the statements following the associated THEN are executed. If none of the ELSEIF conditions is true, the statements following the ELSE are executed. After the statements following a THEN or ELSE are executed, the program continues with the statement following the END IF. The ELSE and ELSEIF blocks are both optional. You can have as many ELSEIF clauses as you would like in a block IF. Any of the statement blocks can contain nested block IF statements. BASIC looks at what appears after the THEN keyword to determine whether or not an IF statement is a block IF. If anything other than a comment appears after THEN, the statement is treated as a single-line IF statement. A block IF statement must be the first statement on a line. The ELSE, ELSEIF, and END IF parts of the statement can have only a line number or line label in front of them. The block must end with an END IF statement. For more information, see Chapter 1, "Control-Flow Structures" in the Programmer's Guide. See Also SELECT CASE Examples The following examples show the use of single-line and block IF... THEN... ELSE statements. Here is the single-line form. CLS ' Clear screen. DO INPUT "Enter a number greater than 0 and less than 10,000.", X IF X >= 0 AND X < 10000 THEN EXIT DO ELSE PRINT X; "out of range" LOOP IF X<10 THEN Y=1 ELSE IF X<100 THEN Y=2 ELSE IF X<1000 THEN Y=3 ELSE Y=4 PRINT "The number has"; Y; "digits" Here is the block form, which is easier to read and more powerful. CLS ' Clear screen. DO INPUT "Enter a number greater than 0 and less than 100,000.", X IF X >= 0 AND X < 100000 THEN EXIT DO ELSE PRINT X; "out of range" END IF LOOP IF X < 10 THEN Y = 1 ELSEIF X < 100 THEN Y = 2 ELSEIF X < 1000 THEN Y = 3 ELSEIF X < 10000 THEN Y = 4 ELSE Y = 5 END IF PRINT "The number has"; Y; "digits"